#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
	int majorityElement(vector<int>& nums) {
		if (nums.size() == 1) {
			return nums[0];
		}
		int maxCount = 0;
		int count = 1;
		int value = 0;
		sort(nums.begin(),nums.end(), less<int>());
		for (int i = 1; i < nums.size(); i++) {
			(nums[i] == nums[i - 1]) ? (count++) : (count = 1);
			if (maxCount < count) {
				maxCount = count;
				value = nums[i];
			}	
		}
		count = 1;
		int secondCount = 0;
		int valueTwo = 0;
		for (int i = 1; i < nums.size(); i++) {
			(nums[i] == nums[i - 1] && nums[i] != value) ? (count++) : (count = 1);
			if (secondCount < count) {
				secondCount = count;
				valueTwo = nums[i];
			}
		}
		//cout << maxCount << " " << secondCount<<endl;
		if (maxCount == secondCount || 2*maxCount<=nums.size()) {
			return -1;
		}
		else {
			return value;
		}
	}
	
};
int main(void) {
	Solution a;
	///(1,1,1,2,2,2,2)
	vector<int> nums = { 2,2,2,3,3,4,4 };
	cout<<a.majorityElement(nums);

}